package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.trainclassgeneral.NoTrainClassGeneralException;
import com.xinzhitong.www.exception.trainclassnameandno.NoTrainClassNameAndNoException;
import com.xinzhitong.www.exception.trainmanager.InCorrectStateException;
import com.xinzhitong.www.service.TrainService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 培训管理控制器
 *
 * @author 徐鑫
 */
@CrossOrigin
@RestController
@RequestMapping("trainManager")
public class TrainManagerController {

    @Resource
    TrainService trainService;

    /**
     * 搜索获取培训概览
     *
     * @param keyWord      关键词
     * @param credentialId 工种id
     * @param pageNo       页码
     * @param pageSize     页大小
     * @return 培训概览
     */
    @RequestMapping(value = "trains", method = RequestMethod.GET)
    @RequiresRoles(value = {"ADMIN"})
    public Message trains(
            @RequestParam(value = "state", defaultValue = "", required = false) String state,
            @RequestParam(value = "keyWord", defaultValue = "", required = false) String keyWord,
            @RequestParam("credentialId") Integer credentialId,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        String msg = "";
        if (state.equals(""))
            state = null;
        try {
            return Message.createSuc(trainService.getTrainClassGeneral(keyWord, credentialId, state, pageNo, pageSize));
        } catch (NoTrainClassGeneralException e) {
            e.printStackTrace();
            msg = "暂无更多培训信息";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取培训概要
     *
     * @param trainClassTimeId 培训id
     * @return 培训概要
     */
    @RequestMapping(value = "theTrain", method = RequestMethod.GET)
    @RequiresRoles(value = {"ADMIN"})
    public Message theTrain(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg = "";
        try {
            return Message.createSuc(trainService.getTheTrainClassSummary(trainClassTimeId));
        } catch (NoTrainClassNameAndNoException e) {
            e.printStackTrace();
            msg = "培训课次信息已失效";
        }
        return Message.createErr(msg);
    }

    /**
     * 允许发布培训课次
     *
     * @param trainClassTimeId 培训课次id
     * @param text             消息文本
     * @return 提示信息
     */
    @RequestMapping(value = "publishTheTrainClassTime", method = RequestMethod.POST)
    @RequiresRoles(value = {"ADMIN"})
    public Message publishTheTrainClassTime(
            @RequestParam("trainClassTimeId") Integer trainClassTimeId,
            @RequestParam(value = "text", required = false, defaultValue = "default") String text
    ) {
        String msg = "发布成功";
        try {
            trainService.publishTheTrainClassTime(trainClassTimeId, text);
            return Message.createSuc(msg);
        } catch (NoTrainClassNameAndNoException e) {
            e.printStackTrace();
            msg = "培训课次信息已失效";
        } catch (InCorrectStateException e) {
            e.printStackTrace();
            msg = "非待审核状态的培训课次，不得发布";
        }
        return Message.createErr(msg);
    }

    /**
     * 驳回发布培训课次
     *
     * @param trainClassTimeId 培训课次id
     * @param text             消息文本
     * @return 提示信息
     */
    @RequestMapping(value = "rejectTheTrainClassTime", method = RequestMethod.POST)
    public Message rejectTheTrainClassTime(
            @RequestParam("trainClassTimeId") Integer trainClassTimeId,
            @RequestParam(value = "text", required = false, defaultValue = "default") String text
    ) {
        String msg = "驳回成功";
        try {
            trainService.rejectTheTrainClassTime(trainClassTimeId, text);
            return Message.createSuc(msg);
        } catch (NoTrainClassNameAndNoException e) {
            e.printStackTrace();
            msg = "培训课次信息已失效";
        } catch (InCorrectStateException e) {
            e.printStackTrace();
            msg = "非待审核状态的培训课次，不得驳回";
        }
        return Message.createErr(msg);
    }

}
